package com.letv.core.utils;

import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Pattern;

public class H265Utils {
    private static final String TAG = "H265Utils";

    private static final long CPU_FREQUENCE_LIMINT_1000MHZ = 1000000L; // 1GHz

    private static final long CPU_FREQUENCE_LIMINT_1500MHZ = 1500000L; // 1.5GHz

    private static final long CPU_FREQUENCE_LIMINT_1800MHZ = 1800000L; // 1.8GHz

    private static final long CPU_FREQUENCE_LIMINT_800MHZ = 800L; // 800 hz

    private static long hz1300 = 0;
    
    public static boolean isSupport1300() {
    	
        if (hz1300 == 0) {
            hz1300 = getCpuFrequence();
        }
        if (hz1300 >= CPU_FREQUENCE_LIMINT_800MHZ) {
            return true;
        } else {
            LogInfo.log(TAG, " getCpuFrequence() >= CPU_FREQUENCE_LIMINT_800MHZ ");
            return false;
        }
    }

    /**
     * 根据手机配置，判断是否支持H265高清播放
     * 
     * @return
     */
    public static boolean isSupportH265() {
        return getCpuFrequence() >= CPU_FREQUENCE_LIMINT_1000MHZ && ifSupportNeon();
    }

    /**
     * 双核以上支持flv 720p码流
     * 
     * @return
     */
    public static boolean isSupport720p() {
        int cn = getNumCores();
        return (cn >= 2 && getCpuFrequence() >= CPU_FREQUENCE_LIMINT_1500MHZ) ||
                (cn >= 4 && getCpuFrequence() >= CPU_FREQUENCE_LIMINT_1000MHZ);
    }

    /**
     * 双核以上支持flv 1080p码流
     * 
     * @return
     */
    public static boolean isSupport1080p() {
        int cn = getNumCores();
        return cn >= 4 && getCpuFrequence() >= CPU_FREQUENCE_LIMINT_1800MHZ;
    }

    /**
     * 判断服务端是否支持H265专区打开标志
     * 
     * @return
     */
//    public static boolean isServerSupportH265() {
//        DataStatusInfoBean dataStatusInfo = BaseApplication.getInstance().getDataStatusInfo();
//        if (dataStatusInfo != null) {
//            H265Info h265Info = dataStatusInfo.getmH265Info();
//            if (h265Info != null) {
//                if (H265Info.RESULT_SUCCESS.equals(h265Info.getStatus())) {
//                    return true;
//                } else {
//                    return false;
//                }
//            } else {
//                return false;
//            }
//        }
//        return false;
//    }

    /**
     * 获取Cpu核数
     * 
     * @return
     */
    public static int getNumCores() {
        // Private Class to display only CPU devices in the directory listing
        class CpuFilter implements FileFilter {
            @Override
            public boolean accept(File pathname) {
                // Check if filename is "cpu", followed by a single digit number
                return Pattern.matches("cpu[0-9]", pathname.getName());
            }
        }

        try {
            // Get directory containing CPU info
            File dir = new File("/sys/devices/system/cpu/");
            // Filter to only list the devices we care about
            File[] files = dir.listFiles(new CpuFilter());
            // Log.i(TAG, "CPU Count: " + files.length);
            // Return the number of cores (virtual CPU devices)
            return files.length;
        } catch (Exception e) {
            // Print exception
            // Log.i(TAG, "CPU Count: Failed.");
            e.printStackTrace();
            // Default to return 1 core
            return 1;
        }
    }

    /**
     * 获取cpu最大主频
     * 
     * @return
     */

    public static long getCpuFrequence() {
        ProcessBuilder cmd;
        try {
            String[] args = { "/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" };
            cmd = new ProcessBuilder(args);

            Process process = cmd.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = reader.readLine();
            // Log.i(TAG, "cpu主频 =" + line);
            if (line != null) {
                return Long.parseLong(line);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            Log.i(TAG, "获取cpu主频发生错误。");
        }
        return 0;
    }

    /**
     * 获取cpu最小频率
     * 
     * @return
     */
    public static long getMinCpuFreq() {
        ProcessBuilder cmd;
        try {
            String[] args = { "/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq" };
            cmd = new ProcessBuilder(args);

            Process process = cmd.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = reader.readLine();
            // Log.i(TAG, "cpu最小主频 =" + line);
            if (line != null) {
                return Long.parseLong(line);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            // Log.i(TAG, "获取cpu最小主频发生错误。");
        }
        return 0;
    }

    /**
     * 判断ARM是否支持NEON
     * 
     * @return
     */
    public static boolean ifSupportNeon() {
        StringBuilder result = new StringBuilder();
        ProcessBuilder cmd;
        try {
            String[] args = { "/system/bin/cat", "/proc/cpuinfo" };
            cmd = new ProcessBuilder(args);

            Process process = cmd.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String l = null;
            while ((l = reader.readLine()) != null) {
                if (l.indexOf("Features") != -1) {
                    result.append(l);
                }
            }
            // Log.i(TAG, result.toString());
        } catch (IOException ex) {
            result.append("Read InputStream Failure !!!");
            ex.printStackTrace();
        }

        return result.toString().indexOf("neon") != -1;
    }
}
